﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;


namespace SmartMeterParamFrame.Model
{
    public class MeterCollection : ObservableCollection<MeterInfo>
    {

        public MeterCollection()
            : base()
        { }

        public MeterCollection(int Count)
            : base()
        {
            MeterInfo.Reset();
            for (int i = 0; i < Count; i++)
            {
                this.Add(new MeterInfo());
            }
        }

        /// <summary>
        /// 重置当前状态
        /// <param name="PrjIndex">方案项目ID</param>
        /// </summary>
        public void ResetCurrentProjectMsg(int PrjIndex)
        {
            foreach (MeterInfo item in this)
            {
                if (item.CheckYn && item.IsFaItemOk(PrjIndex))
                {
                    item.SetPrjResult(PrjIndex, EnumResult.成功, EnumResult.成功);
                }
                else
                {
                    if (!item.IsFailed)     //如果没有失败，则清空项目描述
                    {
                        item.SetPrjResult(PrjIndex, EnumResult.未操作, EnumResult.未操作);
                        item.CurrentDesc = "";
                    }
                    else
                    {
                        if (item.getPrjResult(PrjIndex, true) == EnumResult.失败)
                        {
                            item.SetPrjResult(PrjIndex, EnumResult.未操作, EnumResult.未操作);
                            item.CurrentDesc = "";
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 是否是新的信息，不存在检定数据
        /// </summary>
        /// <returns></returns>
        public bool IsNew()
        {
            foreach (MeterInfo item in this)
            {
                if (item.CheckYn && item.PrjResultsCount != 0)
                {
                    return false;
                }
            }
            return true;
        }
        /// <summary>
        /// 所有表中当前方案项目存在要检内容
        /// </summary>
        /// <param name="PrjIndex"></param>
        /// <returns></returns>
        public bool IsFaYaoJian(int PrjIndex)
        {
            foreach (MeterInfo item in this)
            {
                if (item.CheckYn)
                {
                    if (!item.IsFailed)
                    {
                        if (item.getPrjResult(PrjIndex, true) != EnumResult.成功) return true;
                    }
                    else   //如果存在失败项目，检查是否是当前项目是失败，如果是就返回要检
                    {
                        if (item.getPrjResult(PrjIndex, true) == EnumResult.失败) return true; 
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// 是否存在要检
        /// </summary>
        /// <returns></returns>
        public bool HaveYaoJian()
        {
            foreach (MeterInfo item in this)
            {
                if (item.CheckYn) return true;
            }
            return false;
        }

        /// <summary>
        /// 设置当前项目（主要是为了表信息对象中显示当前项目的结果）
        /// </summary>
        /// <param name="Index"></param>
        /// <param name="PrjName"></param>
        public void SetCurrentProject(int PrjIndex, string PrjName)
        {
            foreach (MeterInfo item in this)
            {
                //item.CurrentPrjName = PrjName;
                item.SetCurrentPrjResult(PrjIndex);
                item.CurrentDesc = "";
            }
        }
        /// <summary>
        /// 清理对象
        /// </summary>
        public void DataClear()
        {
            ReCreat(this.Count);
        }
        /// <summary>
        /// 重新创建
        /// </summary>
        /// <param name="Count"></param>
        public void ReCreat(int Count)
        {
            base.Clear();

            MeterInfo.Reset();

            for (int i = 0; i < Count; i++)
            {
                this.Add(new MeterInfo());
            }
        }

          /// <summary>
        /// 将所有失败的数据重新设置成 未执行
        /// </summary>
        public void ResetAllUnSuccessToNone()
        {
            foreach (MeterInfo mi in this)
            {
                mi.ResetAllUnSuccessToNone();
            }
        }

        /// <summary>
        /// 清空所有已有的结论
        /// </summary>
        public void ClearAllCheckResults()
        {
            foreach (MeterInfo mi in this)
            {
                mi.ClearAllCheckResult();
            }
        }
    }
}
